<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'

export default {
  name: "Scroll",
  props: {
    probeType: {
      type: Number,
      default: 0
    },
    pullUpLoad: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      scroll: null
    }
  },
  mounted() {
    this.scroll = new BScroll(this.$refs.wrapper, {
      /* 解决无法点击 */
      click: true,
      /* 监听位置 */
      probeType: this.probeType,
      /* 解决无法滚动 */
      observeDOM: true,
      /* 上拉加载事件 */
      pullUpLoad: this.pullUpLoad,
      /* 解决刷新迟缓问题 */
      observeImage: {
        /* 解决服务器压力(进行防抖 节流) */
        debounceTime: 50 //ms
      }
    })

    if (this.probeType === 2 || this.probeType === 3) {
      this.scroll.on('scroll', (pro) => {
        this.$emit('scroll', pro)
      })
    }

    if (this.pullUpLoad) {
      this.scroll.on('pullingUp', () => {
        this.$emit('pulling')
      })
    }
  },
  methods: {
    scrollToUp(x, y, time = 800) {
      this.scroll && this.scroll.scrollTo(x, y, time)
    },
    finishPullUp() {
      this.scroll && this.scroll.finishPullUp()
    }
  }
}
</script>

<style scoped>

</style>
